#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Author:Lijiacai
Email:1050518702@qq.com
===========================================
CopyRight@JackLee.com
===========================================
"""

import datetime
import logging
import os
import sys
import json
import requests
import cx_Oracle
import shortuuid
from hashids import Hashids
from elasticsearch import Elasticsearch
from elasticsearch import helpers

cur_dir = os.path.split(os.path.realpath(__file__))[0]
sys.path.append("%s/.." % cur_dir)
import config

sys.path.append("%s/.." % cur_dir)
from res.lijiacai.utils import tool


class Stations():
    station_key = "d9c227c0-a4d2-40ba-b51b-4e479431708a"

    def __init__(self):
        self.data = None
        self.redis_client = tool.RedisDB(**config.redis_conf).client

    def getStations(self):
        headers = {
            "accessKey": self.station_key
        }
        cityIds = [400000, 610000, 650000]
        out = {}

        res = self.redis_client.get("get_stations")
        if res:
            return json.loads(res)
        else:
            for city in cityIds:
                res = requests.get(
                    f"http://admin.pand-auto.com/Admin/order/person/getStationsByCitycode.do?condition.cityId={city}",
                    headers=headers).json()
                datetimeStr = res.get("cityStations").get("datetimeStr")
                for one in res.get("cityStations").get("stations"):
                    allNum = one.get("allNum", 0)
                    areaId = one.get("areaId", 0)
                    changeNum = one.get("changeNum", 0)
                    chargingNum = one.get("chargingNum", 0)
                    freeNum = one.get("freeNum", 0)
                    latitude = one.get("latitude", 0)
                    longitude = one.get("longitude", 0)
                    overNum = one.get("overNum", 0)
                    parkingNum = one.get("parkingNum", 0)
                    stationId = one.get("stationId", 0)
                    stationName = one.get("stationName", "")
                    out[str(stationId)] = {
                        "freeNum": freeNum,
                        "chargingNum": chargingNum,
                        "changeNum": changeNum,
                        "allNum": allNum,
                        "overNum": overNum,
                        "parkingNum": parkingNum,
                        "stationName": stationName
                    }
        self.redis_client.set("get_stations", json.dumps(out), ex=None, px=None, nx=False, xx=False)
        return out

    def go(self):
        self.getStations()


if __name__ == '__main__':
    Stations().go()
